export default class IndexedDB {
  // 打开数据库，没有就会创建数据库
  open(hubName, version, tableName, opt) {
    return new Promise((resolve, reject) => {
      const request = window.indexedDB.open(hubName, version)

      // 创建数据表时调用
      request.onupgradeneeded = function (event) {
        const db = event.target.result
        if (!db.objectStoreNames.contains(tableName)) {
          db.createObjectStore(tableName, opt)
        }
      }

      // 打开错误
      request.onerror = function (event) {
        console.error('indexDb: 数据库打开报错:', event.target.error)
        reject(event.target.error)
      }

      // 打开成功
      request.onsuccess = function (event) {
        console.log('indexDb: 数据库打开成功 ---> ' + tableName)
        resolve(request.result)
      }
    })
  }

  // 检查对象存储是否存在
  async checkObjectStoreExists(db, storeName) {
    return db.objectStoreNames.contains(storeName)
  }

  // 增加数据
  async addData(tableName, data, db) {
    return new Promise((resolve, reject) => {
      const transaction = db.transaction([tableName], 'readwrite')
      const objectStore = transaction.objectStore(tableName)
      const request = objectStore.add(data)

      request.onsuccess = function (event) {
        console.log('indexDb: 数据写入成功')
        resolve(event.target.result)
      }

      request.onerror = function (event) {
        console.error('indexDb: 数据写入失败:', event.target.error)
        reject(event.target.error)
      }
    })
  }

  // 读取数据(查找)
  findByOpt(tableName, id, db) {
    return new Promise((resolve, reject) => {
      const transaction = db.transaction([tableName], 'readonly')
      const objectStore = transaction.objectStore(tableName)
      const request = objectStore.get(id)

      request.onsuccess = function (event) {
        if (request.result) {
          resolve(request.result)
        } else {
          reject('indexDb: 未找到数据')
        }
      }

      request.onerror = function (event) {
        console.error('indexDb: 查询失败:', event.target.error)
        reject(event.target.error)
      }
    })
  }

  // 更新数据
  updateByTableName(tableName, data, db) {
    return new Promise((resolve, reject) => {
      const transaction = db.transaction([tableName], 'readwrite')
      const objectStore = transaction.objectStore(tableName)
      const request = objectStore.put(data)

      request.onsuccess = function (event) {
        console.log('indexDb: 数据更新成功')
        resolve(event.target.result)
      }

      request.onerror = function (event) {
        console.error('indexDb: 数据更新失败:', event.target.error)
        reject(event.target.error)
      }
    })
  }

  // 删除数据
  removeData(tableName, id, db) {
    return new Promise((resolve, reject) => {
      const transaction = db.transaction([tableName], 'readwrite')
      const objectStore = transaction.objectStore(tableName)
      const request = objectStore.delete(id)

      request.onsuccess = function (event) {
        // console.log('数据删除成功')
        resolve(event.target.result)
      }

      request.onerror = function (event) {
        console.error('indexDb: 数据删除失败:', event.target.error)
        reject(event.target.error)
      }
    })
  }

  // 查询数据是否存在
  exists(db, tableName, key) {
    return new Promise((resolve, reject) => {
      const transaction = db.transaction([tableName], 'readonly')
      const objectStore = transaction.objectStore(tableName)
      const request = objectStore.get(key)

      request.onsuccess = function (event) {
        const result = event.target.result
        resolve(result !== undefined ? result : false)
      }

      request.onerror = function (event) {
        console.error('indexDb: 查询失败:', event.target.error)
        reject(event.target.error)
      }
    })
  }

  // 清空对象存储
  clearObjectStore(tableName, db) {
    return new Promise((resolve, reject) => {
      const transaction = db.transaction([tableName], 'readwrite')
      const objectStore = transaction.objectStore(tableName)
      const request = objectStore.clear()

      request.onsuccess = function (event) {
        // console.log('对象存储清空成功')
        resolve(event.target.result)
      }

      request.onerror = function (event) {
        console.error('indexDb: 对象存储清空失败:', event.target.error)
        reject(event.target.error)
      }
    })
  }
}
